import React from 'react';
import { observer, inject } from 'mobx-react';
import * as _ from '../../util/public/common';
import WfForm_Public  from '../../util/public/interface';


@observer
export default class InsertScanCode extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            codetype:"",
            relatefield:"",
            scancodeData:"",
            hidetext:"",
            url:""
        }
    }

    componentWillMount(){
        const { jsonparam,otherparam } = this.props;
        const { isPreView } = otherparam;
        //兼容老的扫描码 如果scancodeSrc没有 或者为空  则为老数据
        let scancodeData = "";
        if (jsonparam.relatefield &&(typeof (jsonparam.scancodeData)==="undefined"||jsonparam.scancodeData==="")) {
            let fieldarr = jsonparam.relatefield.split(',');
            _.forEach(fieldarr, function(value) {
                if (value==="requestid"||value==="requestname"){
                    scancodeData +=  '$'+value+'$';
                } else {
                    let fieldname = WfForm_Public.getFieldInfo(value).fieldname;
                    scancodeData +=  '$'+fieldname+'$';
                }
            });

        }else {
            scancodeData = jsonparam.scancodeData?jsonparam.scancodeData:"";  //对应数据
        }
        let codetype = jsonparam.codetype ? jsonparam.codetype :"";   //1:二维码 2：条形码
        let hidetext = jsonparam.hidetext ? jsonparam.hidetext :"";  //条形码是否显示数据文本

        let msg = this.calculateRelateValue(scancodeData);
        if (isPreView==="1")
            msg = "preview";
        //处理url中含有# 和%的问题
        msg = msg.replace(new RegExp("%","gm"),"p_weaver_p");
        msg = msg.replace(new RegExp("#","gm"),"n_weaver_n");
        msg = msg.replace(new RegExp("<","gm"),"li_weaver_li");
        msg = msg.replace(new RegExp(">","gm"),"b_weaver_b");
        msg = msg.replace(new RegExp("&","gm"),"and_weaver_and");
        msg = msg.replace(new RegExp("=","gm"),"eq_weaver_eq");
        msg = msg.replace(new RegExp("\\+","gm"),"add_weaver_add");

        let url ="";
        if (codetype==='1'){
            url = "/workflow/createQRCode?firstParam=1";
            if(msg==="")
                msg = "空";
            msg = encodeURI(msg);
            url += "&msg="+msg;
        }else if (codetype==='2' &&_.trim(msg)!==""){
            if(/.*[\u4e00-\u9fa5]+.*$/.test(msg)){  //校验字符串是否包含中文
                url = "/workflow/exceldesign/image/illegalBarCode_wev8.png";
            }else {
                url = "/workflow/createWfBarCode?firstParam=1";
                url += "&type=code128&qz=6&msg="+msg;
                if (Number(hidetext)===1){
                    url += "&hrp=none";
                }
            }
        }

        this.setState({codetype:codetype,hidetext:hidetext,url:url });
    }

    calculateRelateValue = (scancodeData) =>{
        if (scancodeData==""){
            return "";
        }

        let result =scancodeData;
        //常用参数
        const commonPara = ['requestid', 'requestname']
        commonPara.map((item) => {
            let repalceReg = '\\$' + item + '\\$';

            if (item==="requestid"){
                result = result.replace(new RegExp(repalceReg, "gm"), WfForm_Public.getBaseInfo().requestid);
            } else {
                result = result.replace(new RegExp(repalceReg, "gm"), WfForm_Public.getFieldValue("field-1")+"");
            }
        })

        //匹配固定参数
        let patt = /\$[^(\$\=\[)]+\$/g;;
        let fieldnames = result.match(patt);   //$fieldname$
        //需要去重
        fieldnames = Array.from(new Set(fieldnames));
        _.forEach(fieldnames, (value)=> {
            let fieldname = value.substring(1,value.length-1);   //fieldname
            let fieldid = WfForm_Public.convertFieldNameToId(fieldname);
            let repalceReg = '\\$' + fieldname + '\\$';
            result = result.replace(new RegExp(repalceReg, "gm"), WfForm_Public.getFieldValue(fieldid));
        });
        return result;
    }

    render(){
        const { otherparam } = this.props;
        const { url,codetype } = this.state;
        const {iscreate} = otherparam;
        let typeclass = codetype==='1'?"qrcodeimg":"barcodeimg";

        let showflag =  !(iscreate==='1'&&codetype==='2') &&url!=="";
        return (
            <div>
                {
                    showflag&&<img className={`scancode ${typeclass}`}
                                                   ref={"insertscancode"}
                                                   onLoad={()=>{
                                                       window.scancodeOperate.readyAdjustSize(this.refs.insertscancode)}
                                                   }
                                                   src= {url}
                    />
                }
            </div>
        )
    }
}